package io.xccit.hotelsystem.ui.home;

import android.content.Context;
import android.database.Cursor;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import java.util.List;
import java.util.ArrayList;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.utils.SQLiteHelper;

/**
 * 首页数据绑定
 */
public class HomeViewModel extends ViewModel {
    
    private final MutableLiveData<List<Integer>> bannerImages = new MutableLiveData<>();
    private final MutableLiveData<List<HomeFragment.Hotel>> hotels = new MutableLiveData<>();
    private Context context;

    public HomeViewModel(Context context) {
        this.context = context;
        loadBannerImages();
        loadHotels();
    }
    
    private void loadBannerImages() {
        List<Integer> images = new ArrayList<>();
        images.add(R.drawable.banner1);
        images.add(R.drawable.banner2);
        images.add(R.drawable.banner3);
        images.add(R.drawable.banner4);
        images.add(R.drawable.banner5);
        bannerImages.setValue(images);
    }
    
    private void loadHotels() {
        List<HomeFragment.Hotel> hotelList = new ArrayList<>();
        SQLiteHelper dbHelper = new SQLiteHelper(context);
        Cursor cursor = dbHelper.getAllHotels();
        
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                String desc = cursor.getString(2);
                String price = cursor.getString(3);
                String image = cursor.getString(4);
                
                // 这里需要根据实际图片资源处理
                int imageRes = R.drawable.hotel_home_1;
                if (image.equals("hotel_home_2")) imageRes = R.drawable.hotel_home_2;
                else if (image.equals("hotel_home_3")) imageRes = R.drawable.hotel_home_3;
                else if (image.equals("hotel_home_4")) imageRes = R.drawable.hotel_home_4;
                
                hotelList.add(new HomeFragment.Hotel(name, price, imageRes));
            } while (cursor.moveToNext());
        }
        cursor.close();
        hotels.setValue(hotelList);
    }
    
    public LiveData<List<Integer>> getBannerImages() {
        return bannerImages;
    }
    
    public LiveData<List<HomeFragment.Hotel>> getHotels() {
        return hotels;
    }
}